######################################
# Generate plots conjoint data
######################################

rm(list=ls())

library("ggplot2")
library(gridExtra)
library(ggpubr)
library(grid)

# set working directory
setwd("~/Dropbox/Cerrillos 2017/08_replication")

########################
# left
########################

# load data
d <- read.table("013_centrist_left.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"

  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  

  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_left = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Latent left-wing") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_left

########################
# right
########################

# load data
d <- read.table("014_centrist_right.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_right = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Latent right-wing") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_right

########################
# nocandidate
########################

# load data
d <- read.table("015_centrist_nocandidate.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_nocandidate = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Non-ideological") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_nocandidate

#############################
# Figure 3
#############################

tiff('~/Dropbox/Cerrillos 2017/08_replication/figure3.tiff', units="in", width=25, height=9, res=300, compression = 'lzw')
ggarrange(f_left,f_right,f_nocandidate, nrow = 1)
dev.off()

